package net.hnilica.cisteniulic.dao;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 *
 * @author swosh
 */
public class Postman {

    protected Logger log = Logger.getLogger(getClass().getName());

    public boolean sendActivationMail(String recipient, String aktHash) {
        boolean ok = true;
        Session session = null;
        String subject = "Aktivace";
        // TODO change domain implicit
        String content = "Aktivace <br/><br/>Pro úspěšné dokončení registrace klikněte"
                + " <a href=\"http://localhost:8080/CisteniUlic/activation?aktHashHex=" + aktHash + "\">zde</a>";

        try {
            Context initCtx = new InitialContext();
            session = (Session) initCtx.lookup("java:/comp/env/mail/cistic");


        } catch (Exception ex) {
            log.log(Level.SEVERE, "Look up error", ex);
            ok = false;
            return ok;
        }

        Message message = new MimeMessage(session);

        try {
            InternetAddress to[] = new InternetAddress[1];
            to[0] = new InternetAddress(recipient);
            message.setRecipients(Message.RecipientType.TO, to);
            message.setSubject(subject);
            message.setContent(content, "text/html;charset=UTF-8");
            Transport.send(message);
        } catch (AddressException ex) {
            log.log(Level.SEVERE, null, ex);
            ok = false;
            return ok;
        } catch (MessagingException ex) {
            log.log(Level.SEVERE, null, ex);
            ok = false;
            return ok;
        }

            return ok;
    }


}
